<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020-8-11
 * Time: 18:07
 */

namespace App\Handlers;

use App\Models\AdminUser;
use Cache;
use App\Models\WeekCheckout;
use App\Models\WeekProfitReturn;


class WeekProfitReturnHandler
{
    protected $user;            // 用户数据
    protected $week_checkout;   // 用户周结算数据


    public function __construct()
    {
        // 用户自身 用户佣金结算表数据
        $this->week_checkout = new WeekCheckout();

        $this->user = Cache::get('users');
    }

    public function __call($method, $parame)
    {
        return $this->{$method}(...$parame);
    }

    public static function __callStatic($method, $parame)
    {
        return (new static)->{$method}(...$parame);
    }


    /***
     * 计算用户最大交易手数
     * @param $user 用户数据 obj 集合
     * @return float|int
     */
    protected function jishuanshoushu($u)
    {
        $total_volume = $this->week_checkout->where('user_id', $u->id)
            ->whereBetween('created_at', [getStartOfWeek(), getEndOfWeek(6)])->sum('volume'); // 用户本周交易手数

        if ($u->investment_amount && $u->investment_amount > '0.00') {
            $total_volume_max = (40 / 50000) * $u->investment_amount;// 本周最大手数  50000 40
            if ($total_volume > $total_volume_max) {
                $total_volume = $total_volume_max;
            }
        }
        return $total_volume;
    }


    protected function zituishoushuyinli($user)
    {
        // 周1至6
        list($t1, $t2) = [getStartOfWeek(), getEndOfWeek(6)];

        // 用户数据查询模型
        $user = $this->user->where('id', $user['id'])->first();

        // 用户佣金结算表
        $week_profit_return = WeekProfitReturn::whereBetween('created_at', [$t1, $t2])
            ->where('user_id', $user->id)->first();// 本周循环用户 佣金结算表

        // 如果本周没有数据，创建一条
        if (!$week_profit_return) { // 本周循环用户 表里没有数据 ，创建一条
            $week_profit_return = WeekProfitReturn::create(['user_id' => $user->id]);
        }

        $res = $this->user->where('tjr_id', $user->id)->all(); // 直推用户
        if ($res) {
            switch ($user->users_leader_id) {
                case 0://
                    break;
                case 1:// DD
                    foreach ($res as $k => $v) {
                        if ($v->is_mail == 1) { //可返佣账户
                            $total_volume = self::jishuanshoushu($v);// 用户本周交易手数
                            //dd($total_volume);
                            if ($v->users_leader_id == $user->users_leader_id) { //同级情况
                                $xj_mail_find = $week_profit_return->where(['user_id' => $v->id])
                                    ->whereBetween('created_at', [$t1, $t2])->first();

                                if ($xj_mail_find) { //之前得处理过 拿10%
                                    $week_profit_return->increment('tj_profit',
                                        ($xj_mail_find->zt_volume + $xj_mail_find->yj_volume + $xj_mail_find->zt_profit + $xj_mail_find->yj_profit + $xj_mail_find->tj_profit) * 0.1); //同级拿10%
                                }
                            }
                            $week_profit_return->increment('zt_volume',
                                $total_volume * $user->users_leader['number']); //总手数*单笔价
                        }
                    }
                    break;
                case 2:// MDD
                    foreach ($res as $k => $v) {
                        $total_volume = self::jishuanshoushu($v);// 用户本周交易手数

                        // 周结算记录表 本周收益
                        $total_profit = $this->week_checkout->where('user_id', $v->id)
                            ->whereBetween('created_at', [$t1, $t2])->first()->profit; //本周收益
                        if ($total_profit < 0) {
                            $total_profit = 0;
                        }

                        $zt_profit = $total_profit * ($user->users_leader['abonus']); // 分红比例

                        if ($v->is_mail == 1) {  //可返佣账户
                            $week_profit_return->increment('zt_volume',
                                ($total_volume) * $user->users_leader['number']); //总手数*单笔价
                            $week_profit_return->increment('zt_profit', $zt_profit); //分红
                        }
                        if ($v->users_leader_id == $user->users_leader_id && $v->is_mail == 1) {//直推代理等级 = 当前用户代理等级
                            $xj_mail_find = $week_profit_return->where('user_id', $v->id)
                                ->whereBetween('created_at', [$t1, $t2])->first();

                            if ($xj_mail_find) {
                                $week_profit_return->increment('tj_profit',
                                    ($xj_mail_find->zt_volume + $xj_mail_find->yj_volume
                                        + $xj_mail_find->zt_profit + $xj_mail_find->yj_profit
                                        + $xj_mail_find->tj_profit) * 0.1); //同级拿10%
                            }

                        } elseif ($v->users_leader_id < $user->users_leader_id) {//直推代理等级 < 当前用户代理等级
                            $xxj_list = getUserTree2($this->user, $v->id, 0, $user->users_leader_id);

                            if ($xxj_list) {
                                $tj_new_list = get_key_type($xxj_list, 2);

                                if ($tj_new_list) {
                                    foreach ($tj_new_list as $new_v) {

                                        $xxj_user_info = $this->user->where('id', $new_v)->first();

                                        if ($xxj_user_info->users_leader_id == $user->users_leader_id
                                            && $xxj_user_info->is_mail == 1) {

                                            $xxj_mail_find = $week_profit_return->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->first();// 用户周结算数据

                                            if ($xxj_mail_find) {
                                                $week_profit_return->increment('tj_profit',
                                                    ($xxj_mail_find->zt_volume + $xxj_mail_find->yj_volume + $xxj_mail_find->zt_profit + $xxj_mail_find->yj_profit + $xxj_mail_find->tj_profit) * 0.1); //同级拿10%
                                            }
                                        }
                                    }
                                }

                                $xj_new_list = get_key_type($xxj_list, 1);

                                if ($xj_new_list) {
                                    foreach ($xj_new_list as $xxj) {

                                        $xxj_user_info = $user->where('id', $xxj)->first();

                                        if ($xxj_user_info->is_mail == 1) {
                                            $xxj_volume_rate = $xxj_user_info->users_leader['number'] ?? 4;

                                            $xxj_volume = self::jishuanshoushu($xxj_user_info);// 用户本周交易手数

                                            $xxj_profit = $this->week_checkout->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->sum('profit'); //本周收益
                                            if ($xxj_profit < 0) {
                                                $xxj_profit = 0;
                                            }

                                            $week_profit_return->increment('yj_volume',
                                                ($xxj_volume) * ($user->users_leader['number'] - $xxj_volume_rate)); //直推手数佣金

                                            $week_profit_return->increment('yj_profit',
                                                $xxj_profit * ($user->users_leader['abonus'])); //直推手数佣金
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 3:// PDD
                    foreach ($res as $xj) {
                        $total_volume = self::jishuanshoushu($xj);// 用户本周交易手数

                        // 周结算记录表 本周收益
                        $total_profit = $this->week_checkout->where('user_id', $xj->id)
                            ->whereBetween('created_at', [$t1, $t2])->sum('profit'); //本周收益

                        if ($total_profit < 0) {
                            $total_profit = 0;
                        }

                        $zt_profit = $total_profit * ($user->users_leader['abonus']); //分红比例
                        if ($xj->is_mail == 1) {   //可返佣账户
                            $week_profit_return->increment('zt_volume',
                                ($total_volume) * $user->users_leader['number']); // 直推手数 总手数*单笔价
                            $week_profit_return->increment('zt_profit', $zt_profit); // 直推业绩
                        }

                        if ($xj->users_leader_id == $user->users_leader_id && $xj->is_mail == 1) {//直推代理等级 = 当前用户代理等级  同级佣金

                            $xj_mail_find = $week_profit_return->where('user_id', $xj->id)
                                ->whereBetween('created_at', [$t1, $t2])->first();

                            if ($xj_mail_find) {
                                $week_profit_return->increment('tj_profit',
                                    ($xj_mail_find->zt_volume + $xj_mail_find->yj_volume
                                        + $xj_mail_find->zt_profit + $xj_mail_find->yj_profit
                                        + $xj_mail_find->tj_profit) * 0.1); //同级拿10%
                            }
                        } elseif ($xj->users_leader_id < $user->users_leader_id) {

//                            if($user->id == 3){
//                               echo $xj->id . '<br>';
//                            }

                            $xxj_list = getUserTree2($this->user, $xj->id, 0, $user->users_leader_id);

                            if ($xxj_list) {
                                $tj_new_list = get_key_type($xxj_list, 2);

                                if ($tj_new_list) {

                                    foreach ($tj_new_list as $new_v) {
                                        $xxj_user_info = $user->where('id', $new_v)->first();

                                        if ($xxj_user_info->users_leader_id == $user->users_leader_id && $xxj_user_info->is_mail == 1) {//直推代理id = 当前用户id

                                            $xxj_mail_find = $week_profit_return->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->first();// 用户周结算数据

                                            if ($xxj_mail_find) {
                                                $week_profit_return->increment('tj_profit', ($xxj_mail_find->zt_volume +
                                                        $xxj_mail_find->yj_volume + $xxj_mail_find->zt_profit +
                                                        $xxj_mail_find->yj_profit + $xxj_mail_find->tj_profit) * 0.1); //同级拿10%
                                            }
                                        }
                                    }
                                }

                                $xj_new_list = get_key_type($xxj_list, 1);
                                if ($xj_new_list) {

                                    $is_mib = $user->whereIn('id', $xxj_list)->where('users_leader_id', 2)->get();
                                    $zj_mib = $user->where('id', $xj->id)->where('users_leader_id', 2)->first();

                                    if ($is_mib || $zj_mib) {//如果存在MDD
                                        $pib_volume_rate = $user->users_leader['number'] - $user->users_leader->where('id',
                                                2)->sum('number');
                                        $pib_fh_rate     = $user->users_leader['abonus'] - $user->users_leader->where('id',
                                                2)->sum('abonus');
                                    } else {
                                        $pib_volume_rate = $user->users_leader['number'] - $user->users_leader->where('id',
                                                1)->sum('number');
                                        $pib_fh_rate     = $user->users_leader['abonus'];
                                    }

                                    foreach ($xxj_list as $xxj) {
                                        $xxj_user_info = $user->where('id', $xxj)->first();

                                        if ($xxj_user_info->is_mail == 1 && $xxj_user_info->users_leader_id < $user->users_leader_id) {
                                            $xxj_volume = self::jishuanshoushu($xxj_user_info);// 用户本周交易手数

                                            // 周结算记录表 本周收益
                                            $xxj_profit = $this->week_checkout->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->sum('profit'); //本周收益

//                                            echo '<pre>';
//                                            print_r($xxj_volume);
//                                            echo '-';
//                                            print_r($xxj_profit);
//                                            echo '<br>';
                                            if ($xxj_profit < 0) {
                                                $xxj_profit = 0;
                                            }
//                                            echo '<br>';
//                                            print_r($xxj);
//                                            echo '<br>';
//                                            print_r($xxj_profit);
//                                            echo '<br>';
//                                            print_r($user->id);
//                                            echo '<br>';
//                                            print_r($xxj_profit*($pib_fh_rate));
//                                            echo '__________';

                                            $week_profit_return->increment('yj_volume',
                                                ($xxj_volume) * $pib_volume_rate); //直推手数佣金
                                            //echo $week_profit_return->yj_profit . '_<br>';
                                            $week_profit_return->increment('yj_profit',
                                                $xxj_profit * ($pib_fh_rate)); //直推手数佣金
                                            //echo $week_profit_return->yj_profit . '_*****<br>';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
                case 4:// DDD
                    foreach ($res as $xj) {
                        $total_volume = self::jishuanshoushu($xj);// 用户本周交易手数

                        // 周结算记录表 本周收益
                        $total_profit = $this->week_checkout->where('user_id', $xj->id)
                            ->whereBetween('created_at', [$t1, $t2])->sum('profit'); //本周收益

                        if ($total_profit < 0) {
                            $total_profit = 0;
                        }

                        $zt_profit = $total_profit * ($user->users_leader['abonus']); //分红比例
                        if ($xj->is_mail == 1) {   //可返佣账户
                            $week_profit_return->increment('zt_volume',
                                ($total_volume) * $user->users_leader['number']); // 直推手数 总手数*单笔价
                            $week_profit_return->increment('zt_profit', $zt_profit); // 直推业绩
                        }

                        if ($xj->users_leader_id == $user->users_leader_id && $xj->is_mail == 1) {//直推代理等级 = 当前用户代理等级  同级佣金

                            $xj_mail_find = $week_profit_return->where('user_id', $xj->id)
                                ->whereBetween('created_at', [$t1, $t2])->first();

                            if ($xj_mail_find) {
                                $week_profit_return->increment('tj_profit',
                                    ($xj_mail_find->zt_volume + $xj_mail_find->yj_volume
                                        + $xj_mail_find->zt_profit + $xj_mail_find->yj_profit
                                        + $xj_mail_find->tj_profit) * 0.1); //同级拿10%
                            }
                        } elseif ($xj->users_leader_id < $user->users_leader_id) {

//                            if($user->id == 3){
//                               echo $xj->id . '<br>';
//                            }

                            $xxj_list = getUserTree2($this->user, $xj->id, 0, $user->users_leader_id);

                            if ($xxj_list) {
                                $tj_new_list = get_key_type($xxj_list, 2);

                                if ($tj_new_list) {

                                    foreach ($tj_new_list as $new_v) {
                                        $xxj_user_info = $user->where('id', $new_v)->first();

                                        if ($xxj_user_info->users_leader_id == $user->users_leader_id && $xxj_user_info->is_mail == 1) {//直推代理id = 当前用户id

                                            $xxj_mail_find = $week_profit_return->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->first();// 用户周结算数据

                                            if ($xxj_mail_find) {
                                                $week_profit_return->increment('tj_profit', ($xxj_mail_find->zt_volume +
                                                        $xxj_mail_find->yj_volume + $xxj_mail_find->zt_profit +
                                                        $xxj_mail_find->yj_profit + $xxj_mail_find->tj_profit) * 0.1); //同级拿10%
                                            }
                                        }
                                    }
                                }

                                $xj_new_list = get_key_type($xxj_list, 1);
                                if ($xj_new_list) {

                                    $is_mib = $user->whereIn('id', $xxj_list)->where('users_leader_id', 2)->get();
                                    $zj_mib = $user->where('id', $xj->id)->where('users_leader_id', 2)->first();

                                    $is_pib = $user->whereIn('id', $xxj_list)->where('users_leader_id', 3)->get();
                                    $zj_pib = $user->where('id', $xj->id)->where('users_leader_id', 3)->first();

                                    if ($is_pib || $zj_pib) {//如果存在PDD
                                        $pib_volume_rate = $user->users_leader['number'] - $user->users_leader->where('id',
                                                3)->sum('number');
                                        $pib_fh_rate     = $user->users_leader['abonus'] - $user->users_leader->where('id',
                                                3)->sum('abonus');
                                    } else {
                                        if ($is_mib || $zj_mib) {//如果存在MDD
                                            $pib_volume_rate = $user->users_leader['number'] - $user->users_leader->where('id',
                                                    2)->sum('number');
                                            $pib_fh_rate     = $user->users_leader['abonus'] - $user->users_leader->where('id',
                                                    2)->sum('abonus');
                                        } else {
                                            $pib_volume_rate = $user->users_leader['number'] - $user->users_leader->where('id',
                                                    1)->sum('number');
                                            $pib_fh_rate     = $user->users_leader['abonus'];
                                        }
                                    }

                                    foreach ($xxj_list as $xxj) {
                                        $xxj_user_info = $user->where('id', $xxj)->first();

                                        if ($xxj_user_info->is_mail == 1 && $xxj_user_info->users_leader_id < $user->users_leader_id) {
                                            $xxj_volume = self::jishuanshoushu($xxj_user_info);// 用户本周交易手数

                                            // 周结算记录表 本周收益
                                            $xxj_profit = $this->week_checkout->where('user_id', $xxj_user_info->id)
                                                ->whereBetween('created_at', [$t1, $t2])->sum('profit'); //本周收益

//                                            echo '<pre>';
//                                            print_r($xxj_volume);
//                                            echo '-';
//                                            print_r($xxj_profit);
//                                            echo '<br>';
                                            if ($xxj_profit < 0) {
                                                $xxj_profit = 0;
                                            }
//                                            echo '<br>';
//                                            print_r($xxj);
//                                            echo '<br>';
//                                            print_r($xxj_profit);
//                                            echo '<br>';
//                                            print_r($user->id);
//                                            echo '<br>';
//                                            print_r($xxj_profit*($pib_fh_rate));
//                                            echo '__________';

                                            $week_profit_return->increment('yj_volume',
                                                ($xxj_volume) * $pib_volume_rate); //直推手数佣金
                                            //echo $week_profit_return->yj_profit . '_<br>';
                                            $week_profit_return->increment('yj_profit',
                                                $xxj_profit * ($pib_fh_rate)); //直推手数佣金
                                            //echo $week_profit_return->yj_profit . '_*****<br>';
                                        }
                                    }
                                }
                            }
                        }
                    }
                    break;
            }
        } else {
        }
    }


}
